package uy.edu.um.ltic.client.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import uy.edu.um.ltic.client.communication.MessageRemoteClientMgr;
import uy.edu.um.ltic.commons.EmployeeVO;
import uy.edu.um.ltic.commons.ExceptionComm;
import uy.edu.um.ltic.commons.MessageVO;
import uy.edu.um.ltic.exception.DataBaseException;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Descargas extends JDialog {

	private final JPanel contentPanel = new JPanel();

	

	/**
	 * Create the dialog.
	 */
	public Descargas(EmployeeVO sender, EmployeeVO receiver) {
		setTitle("Descargas");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		
		
		JScrollPane scrollPane = new JScrollPane();
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 404, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		JList<Integer> filesList = new JList<Integer>();
		ArrayList<Integer> fileIdArray = new ArrayList<Integer>();
		ArrayList<Integer> fileIdArray1 = new ArrayList<Integer>();
		ArrayList<Integer> fileIdArray2 = new ArrayList<Integer>();
		try {
			fileIdArray1 = MessageRemoteClientMgr.getInstance().getFilesFromOneEmployeeToAnother(sender.getnEmployee(), receiver.getnEmployee());
			fileIdArray2 = MessageRemoteClientMgr.getInstance().getFilesFromOneEmployeeToAnother(receiver.getnEmployee(), sender.getnEmployee());
			
		} catch (ExceptionComm | DataBaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(Integer int1 : fileIdArray1){
			fileIdArray.add(int1);
		}
		for(Integer int2 : fileIdArray2){
			fileIdArray.add(int2);
		}
		filesList.setCellRenderer(new ListCellRenderer<Integer>() {

			@Override
			public Component getListCellRendererComponent(
					JList<? extends Integer> list, Integer value, int index,
					boolean isSelected, boolean cellHasFocus) {
				
				DescargasRenderPanel oReturn = new DescargasRenderPanel(value);
				if(isSelected){
					oReturn.setBackground(Color.LIGHT_GRAY);
				}
				
				return oReturn;
			}
		});
		
		// Add a listener for mouse clicks
		DefaultListModel<Integer> listModel = new DefaultListModel<Integer>();
		for (int i=0; i<fileIdArray.size(); i++){
			listModel.add(i,fileIdArray.get(i) );
		}
		filesList.setModel(listModel);
				filesList.addMouseListener(new MouseAdapter() {
				    public void mouseClicked(MouseEvent evt) {
				    	
				    	
				        JList list = (JList)evt.getSource();
				        
				        if (evt.getClickCount() == 2) {          // Double-click
				          
				        	 
				    	    FileOutputStream fileOuputStream;
							try {
								
								byte[] fileBytes = MessageRemoteClientMgr.getInstance().getFile((Integer) list.getModel().getElementAt(list.getSelectedIndex()));
								String fileName =  MessageRemoteClientMgr.getInstance().getNameOfFile((Integer) list.getModel().getElementAt(list.getSelectedIndex()));
								String userDir = System.getProperty("user.home") + "/Desktop/" + fileName;
								fileOuputStream = new FileOutputStream(userDir);
					    	    fileOuputStream.write(fileBytes);
					    	    fileOuputStream.close();

					    	    
					    	    
							} catch (IOException | ExceptionComm | DataBaseException e) {

								e.printStackTrace();
							}    
				    	   
				           
				        } 
				    }
				});
		
		
		
		
		
		
		scrollPane.setViewportView(filesList);
		contentPanel.setLayout(gl_contentPanel); 
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
